package com.airsystem.pos.bengkel.entity.wrapper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.airsystem.pos.bengkel.entity.Tagihan;
import com.airsystem.pos.bengkel.util.DateUtil;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class TagihanWrapper extends Tagihan {
	private final String periode;

	private String periodeMonth;
	private String periodeYear;

	private int periodeMonthIndex;
	private int periodeYearIndex;

	private final List<Tagihan> tagihans;

	private BigDecimal total;

	public TagihanWrapper(Tagihan tagihan) {
		this.periode = tagihan.getPeriode();
		this.tagihans = new ArrayList<Tagihan>();

		String[] periodeSplit = periode.split("\\/");

		periodeMonth = periodeSplit[0];
		periodeYear = periodeSplit[1];
		periodeMonthIndex = 0;
		periodeYearIndex = 0;

		addTagihan(tagihan);
	}

	public String getPeriode() {
		return periode;
	}

	public int getMonthIndex() {
		if (periodeMonthIndex == 0) {
			int index = 0;

			for (String month : DateUtil.getMonth()) {
				if (periodeMonth.equals(month)) {
					periodeMonthIndex = index + 1;

					break;
				}

				index++;
			}
		}

		return periodeMonthIndex;
	}

	public int getYearIndex() {
		if (periodeYearIndex == 0) {
			int index = 0;

			for (String year : DateUtil.getYear()) {
				if (periodeYear.equals(year)) {
					periodeYearIndex = index + 1;

					break;
				}

				index++;
			}
		}

		return periodeYearIndex;
	}

	public List<Tagihan> getTagihans() {
		return tagihans;
	}

	public void addTagihan(Tagihan tagihan) {
		this.tagihans.add(tagihan);

		setTotal(new BigDecimal(tagihan.getBiaya()));
	}

	public BigDecimal getTotal() {
		return total;
	}

	public void setTotal(BigDecimal total) {
		if (this.total == null) {
			this.total = new BigDecimal(0);
		}

		this.total = this.total.add(total);
	}
}